package com.jmu.medicineknows.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmu.medicineknows.entity.LoginUser;
import com.jmu.medicineknows.entity.Result;
import com.jmu.medicineknows.entity.User;
import com.jmu.medicineknows.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",username);
        User user = userMapper.selectOne(queryWrapper);

        if(user == null){
            throw new RuntimeException("用户名或密码错误");
        }

        List<String> list = userMapper.selectPermsByUserId(user.getId());

        return new LoginUser(user,list);
    }
}
